package com.zs.controller;

import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@ResponseBody
@Controller
@RequestMapping("/cookie")
public class CookieController {
    // 存储一个数据到cookie
    // get cookie/save -> name = root
    @GetMapping("/save")
    public String save(HttpServletResponse response){
        Cookie cookie = new Cookie("name", "root");
        response.addCookie(cookie);
        return "save success";
    }

    /*
    * 获取cookie
    * 1 尽量声明cookie同名的string类型参数
    *  2 。必须使用@CookieValue()注解
    */
    // 获取cookie的数据
    // get cookie/show ->name -> 客户端

    @GetMapping("/show")
    public String show(@CookieValue(value = "name",required = false) String name){
        System.out.println("name:"+name);
        return name;

    }
}
